import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ViteCompression from 'vite-plugin-compression'
import styleImport from 'vite-plugin-style-import';
import prismjs from "vite-plugin-prismjs";

const timestamp = new Date().getTime()
let apiUrl = "http://go.psn.asia"
const plugins = [];

export default defineConfig({
  base: '/',
  build: {
    chunkSizeWarningLimit: 1000,
    outDir: "web",
    assetsDir: "assets", //指定静态资源存放路径
    // assetsPublicPath:'',
    sourcemap: true, //是否构建source map 文件
    rollupOptions: {
      output: {
        entryFileNames: `assets/[name].${timestamp}.js`,
        chunkFileNames: `assets/[name].${timestamp}.js`,
        assetFileNames: `assets/[name].${timestamp}.[ext]`
      }
    },
    minify:"terser",
    terserOptions: {
      // 生产环境移除console
      compress: {
        drop_console: true,
        drop_debugger: true,
      }
    }
  },
  css: {
    postcss: {
      plugins: [
        {
          postcssPlugin: 'internal:charset-removal',
          AtRule: {
            charset: (atRule) => {
              if (atRule.name === 'charset') {
                atRule.remove();
              }
            }
          }
        }
      ]
    }
  },
  plugins: [
    vue(),
    ViteCompression(),
    //按需引入
    styleImport({
      libs: [
          {
            libraryName: 'element-plus',
            esModule: true,
            resolveStyle: (name) => {
              return `element-plus/lib/theme-chalk/${name}.css`;
            },
            resolveComponent: (name) => {
              return `element-plus/lib/${name}`;
            }
          }
      ]
    }),
	prismjs({
	  languages: ["js","php","go","html","bash","css","sql"],
	  //  languages: 'all',
	  plugins: ["line-numbers","copy-to-clipboard"], //配置显示行号插件
	  theme: "tomorrow", //主题名称
	  css: true,
	}),

    ...plugins
  ],

  //配置跨域
  server: {
    open: false,  //启动项目后打开浏览器
    port: 3000,   //端口
    proxy: {
      '/blog': {
        target: apiUrl,  //API服务地址
        changeOrigin: true,             //开启跨域
      },
    }
  }
})
